package com.elecbook.base.util;


import com.elecbook.base.exception.BaseInfoException;

import java.util.function.Function;

public class VerificationUtil {

    public static <T> void verify(T object, Function<T, Integer> userIdExtractor) {
        Integer extractedUserId = userIdExtractor.apply(object);
        Integer currentUserId = SecurityUtil.getCurrentId().intValue();
        if (!extractedUserId.equals(currentUserId)) {
            throw new SecurityException("User ID mismatch. Access denied.");
        }
    }

    public static <T> void verifyValue(T value1, T value2, String msg) {
        if (!value1.equals(value2)) {
            throw new BaseInfoException(msg);
        }
    }

    public static <T> void verifyValue(T value1, T value2) {
        if (!value1.equals(value2)) {
            throw new BaseInfoException("检验数据库内部值的时候发现不一致，此次为非法的操作");
        }
    }
}
